// router/index.ts
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import MyProjectsView from '../views/MyProjectsView.vue';
import FlowEditorView from '../views/FlowEditorView.vue';
import ProfileView from '../views/ProfileView.vue';
import TeamView from '../views/TeamView.vue';
import LoginView from '../views/LoginView.vue';
import RegisterView from '../views/RegisterView.vue';
import ChatView from '../views/ChatView.vue';
import TeamCreateView from '@/views/TeamCreateView.vue';
import TeamJoinView from '@/views/TeamJoinView.vue';
import TeamDetailView from '@/views/TeamDetailView.vue';
import TeamManageView from '@/views/TeamManageView.vue';
import ProjectPreview from '../views/ProjectPreview.vue';
import { useUserStore } from '../stores/userStore'; // 引入 user store

const routes = [
  { path: '/', component: HomeView },
  { path: '/flow', component: FlowEditorView },
  { path: '/chat', component: ChatView },
  { path: '/myprojects', component: MyProjectsView },
  { path: '/profile', component: ProfileView },
  { path: '/profile/:visitorID/:visitorName/:id/:name',name: 'Profile', component: ProfileView },
  { path: '/team', component: TeamView },
  { path: '/team/create', component: TeamCreateView },
  { path: '/team/join', component: TeamJoinView },
  { path: '/team/detail', component: TeamDetailView },
  { path: '/team/manage', component: TeamManageView },
  { path: '/login', component: LoginView },
  { path: '/register', component: RegisterView },
  { path: '/project/:visitorID/:visitorName/:id',name: 'ProjectPreview',component: ProjectPreview}
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  
  // 如果用户没有登录，访问 `/profile` 或 `/flow` 跳转到登录页面
  if ((to.path === '/profile' || to.path === '/flow' || to.path === '/myprojects' || to.path === '/team' ) && !userStore.isLoggedIn) {
    next('/login'); // 跳转到登录页面
  } else {
    next(); // 正常跳转
  }
});

export default router;
